package bg.unisofia.fmi.tis.jbpm;

import java.util.Map;

import org.jbpm.api.activity.ActivityExecution;
import org.jbpm.api.activity.ExternalActivityBehaviour;

import bg.unisofia.fmi.tis.model.Notification;
import bg.unisofia.fmi.tis.model.Thesis;
import bg.unisofia.fmi.tis.service.ModelService;

public class SubmitProposal implements ExternalActivityBehaviour {

	@Override
	public void signal(ActivityExecution execution, String signalName, Map<String, ?> parameters)
			throws Exception {
		
		execution.take(signalName);
	}

	@Override
	public void execute(ActivityExecution execution) throws Exception {
	    
		Integer thesisId = (Integer) execution.getVariable("thesisId");
		
		if (thesisId != null) {
			Thesis thesis = (Thesis) ModelService.findById(Thesis.class, thesisId);
			
			Notification ntfStudent = new Notification(thesis.getStudent(), thesis);
			Notification ntfSupervisor = new Notification(thesis.getSupervisor(), thesis);
			
			ModelService.save(ntfStudent);
			ModelService.save(ntfSupervisor);
			
		} 
		
	    execution.waitForSignal();
	}
}
